package com.dns.customers.aoptest.model;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author :  yulei
 * @data :  2018/6/11 16:06
 * @Version :  1.0
 **/

public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        if(beanName.equals("car")){
            Car car = (Car) bean;
            if(car.getColor()==null){
                System.out.println("调用BeanPostProcessor.postProcessBeforeInitialization()，color为空，设置为默认颜色");
                car.setColor("黑色");
            }

        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
         if(beanName.equals("car")){
             Car car = (Car) bean;
             if(car.getMaxSpeed()>=200){
                 System.out.println("调用BeanPostProcessor.postProcessAfterInitialization(),将maxSpeed调整为200.");
                 car.setMaxSpeed(200);
             }

         }
        return bean;
    }
}
